const http = require("http");
const app = http.createServer();

//连接数据库
const mongoose = require("mongoose", { useNewUrlParser: true });
const url = require("url");

mongoose
  .connect("mongodb://localhost/playground")
  .then(() => {
    console.log("链接数据库成功");
  })
  .catch((err) => {
    console.log("连接失败");
  });

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 2,
    maxlength: 20,
  },
  age: {
    type: Number,
    min: 18,
    max: 80,
  },
  password: String,
  email: String,
  hobbies: [String],
});

//创建规则 实例
let User = mongoose.model("User", userSchema);
//创建链接
app.on("request", (req, res) => {
  // 请求方式
  const method = req.method;
  // 请求地址
  const { pathname } = url.parse(req.url);

  // 判断
  if (method == "GET") {
    if (pathname == "/list") {
        let list = `<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>用户列表</title>
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
        </head>
        <body>
            <div class="container">
                <h6>
                    <a href="add.html" class="btn btn-primary">添加用户</a>
                </h6>
                <table class="table table-striped table-bordered">
                    <tr>
                        <td>用户名</td>
                        <td>年龄</td>
                        <td>爱好</td>
                        <td>邮箱</td>
                        <td>操作</td>
                    </tr>
                    <tr>
                        <td>张三</td>
                        <td>20</td>
                        <td>
                            <span>抽烟</span>
                            <span>喝酒</span>
                            <span>烫头</span>
                        </td>
                        <td>zhangsan@itcast.cn</td>
                        <td>
                            <a href="" class="btn btn-danger btn-xs">删除</a>
                            <a href="" class="btn btn-success btn-xs">修改</a>
                        </td>
                    </tr>
                </table>
            </div>
        </body>
        </html>`
        res.end(list);
    }
  } else if (method == "PSOT") {
  }

});

app.listen(3000);
